package com.example.module_web.viewmodule;

import android.content.Context;
import android.widget.Toast;

import com.example.module_web.R;
import com.example.module_web.beans.Consignment;
import com.example.module_web.beans.Buyer;
import com.example.module_web.beans.DieMeiShi;
import com.example.module_web.beans.JieMianRu;
import com.example.module_web.beans.MonthChart;
import com.example.module_web.beans.Order;
import com.example.module_web.beans.Product;
import com.example.module_web.beans.Result;
import com.example.module_web.beans.WaterBox;
import com.example.module_web.beans.WaterSingleton;
import com.example.module_web.beans.WhitenBox;
import com.example.module_web.beans.WhitenSingleton;
import com.example.module_web.beans.XiuHuXL;
import com.example.module_web.beans.YouthTime;
import com.example.module_web.viewmodule.Net.HttpClient;

import org.json.JSONException;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import Net.Error;
import Net.ResponseHandler;
import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * Created by admin on 2018/3/5.
 */

public class NetData_Web {

    private static volatile NetData_Web mInstance;
    public static NetData_Web getInstance() {
        if (mInstance == null) {
            synchronized (NetData_Web.class) {
                if (mInstance == null) {
                    mInstance = new NetData_Web();
                }
            }
        }
        return mInstance;
    }
    private NetData_Web() {
    }

    public interface Singer<T>{
        void success(T s) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, JSONException;
        void failed(String s);
    }



    //获取订单图片
    public void getPicture(Context context, String id, final Singer<Result> singer){
        HttpClient client=new HttpClient(context);
        Call<Result> call=client.service(ISjun.urltaobao).getPicture(id);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((Result)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }
    //获取详情订单
    public void getOrders(Context context, String id, final Singer<List<Order>> singer){
        HttpClient client=new HttpClient(context);
        Call<List<Order>> call=client.service(ISjun.urltaobao).getOrder(id);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((List<Order>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    //获取订单发货
    public void getConsignments(Context context, String x, final Singer<List<Consignment>> singer){
        HttpClient client=new HttpClient(context);
        Call<List<Consignment>> call=client.service(ISjun.urltaobao).getConsignment(x);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((List<Consignment>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    //获取会员查看
    public void getBuyers(Context context, String x, final Singer<List<Buyer>> singer){
        HttpClient client=new HttpClient(context);
        Call<List<Buyer>> call = client.service(ISjun.urltaobao).getBuyer(x);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((List<Buyer>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    //获取产品查看
    public void getProducts(Context context, String x, final Singer<List<List<Product>>> singer){
        HttpClient client=new HttpClient(context);
        Call<List<List<Product>>> call = client.service(ISjun.urltaobao).getProduct(x);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((List<List<Product>>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    //获取补水套盒
    public void getWaterBoxes(Context context, String x, String id, final Singer<List<WaterBox>> singer){
        HttpClient client = new HttpClient(context);
        Call<List<WaterBox>> call = client.service(ISjun.urltaobao).getWaterBox(x, id);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((List<WaterBox>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    //获取补水单品
    public void getWaterSingletons(Context context, String x, String id, final Singer<List<WaterSingleton>> singer){
        HttpClient client = new HttpClient(context);
        Call<List<WaterSingleton>> call = client.service(ISjun.urltaobao).getWaterSingleton(x, id);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((List<WaterSingleton>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    //获取美白套盒
    public void getWhitenBoxes(Context context, String x, String id, final Singer<List<WhitenBox>> singer){
        HttpClient client = new HttpClient(context);
        Call<List<WhitenBox>> call = client.service(ISjun.urltaobao).getWhitenBox(x, id);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((List<WhitenBox>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    //获取美白单品
    public void getWhitenSingletons(Context context, String x, String id, final Singer<List<WhitenSingleton>> singer){
        HttpClient client = new HttpClient(context);
        Call<List<WhitenSingleton>> call = client.service(ISjun.urltaobao).getWhitenSingleton(x, id);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((List<WhitenSingleton>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    //获取修护系列
    public void getXiuHuXLes(Context context, String x, String id, final Singer<List<XiuHuXL>> singer){
        HttpClient client = new HttpClient(context);
        Call<List<XiuHuXL>> call = client.service(ISjun.urltaobao).getXiuHuXL(x, id);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((List<XiuHuXL>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    //获取青春定格
    public void getYouthTimes(Context context, String x, String id, final Singer<List<YouthTime>> singer){
        HttpClient client = new HttpClient(context);
        Call<List<YouthTime>> call = client.service(ISjun.urltaobao).getYouthTime(x, id);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((List<YouthTime>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    //获取蝶美时
    public void getDieMeiShies(Context context, String x, String id, final Singer<List<DieMeiShi>> singer){
        HttpClient client = new HttpClient(context);
        Call<List<DieMeiShi>> call = client.service(ISjun.urltaobao).getDieMeiShi(x, id);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((List<DieMeiShi>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    //获取洁面乳
    public void getJieMianRues(Context context, String x, String id, final Singer<List<JieMianRu>> singer){
        HttpClient client = new HttpClient(context);
        Call<List<JieMianRu>> call = client.service(ISjun.urltaobao).getJieMianRu(x, id);
        client.request(call, new ResponseHandler() {
            @Override
            public void onSuccess(Object o) throws NoSuchMethodException, IllegalAccessException, JSONException, InvocationTargetException {
                singer.success((List<JieMianRu>)o);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }
    /**订单分析*/
    public void getYearChart(final Context context, String x, final Singer<String> singer){
        HttpClient _httpClient=new HttpClient(context);
        Call<ResponseBody> _call=_httpClient.service(ISjun.urltaobao).getYearChart(x);
        _httpClient.request(_call, new ResponseHandler<ResponseBody>() {
            @Override
            public void onSuccess(ResponseBody IO) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException, JSONException {
                singer.success(IO.string());
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    public void getMonthChart(final Context context, String x, final Singer<List<MonthChart>> singer){
        HttpClient _httpClient=new HttpClient(context);
        Call<List<MonthChart>> _call=_httpClient.service(ISjun.urltaobao).getMonthChart(x);
        _httpClient.request(_call, new ResponseHandler() {
            @Override
            public void onSuccess(Object IO) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException, JSONException {
                singer.success((List<MonthChart>)IO);
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    public void getBuyerLabel(final Context context, String x, final Singer<String> singer){
        HttpClient _httpClient=new HttpClient(context);
        Call<ResponseBody> _call=_httpClient.service(ISjun.urltaobao).getBuyerLabel(x);
        _httpClient.request(_call, new ResponseHandler<ResponseBody>() {
            @Override
            public void onSuccess(ResponseBody IO) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException, JSONException {
                singer.success(IO.string());
            }

            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }

    public  void addProductData(final Context context, String x, String[] data, String id, final Singer<Boolean> singer){
        HttpClient _httpClient=new HttpClient(context);
        Call<Result> _call=_httpClient.service(ISjun.urltaobao).addProductData(x,data[0],data[1],data[2],data[3],data[4],data[5],id);
        _httpClient.request(_call, new ResponseHandler() {
            @Override
            public void onSuccess(Object IO) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException, JSONException {
                if (((Result)IO).getResult().contains("rue")){
                    singer.success(true);
                }else {
                    singer.success(false);
                }

            }
            @Override
            public void onFailure(int code, Error e) {
                singer.failed(e.getMessage());
            }
        });
    }
}
